//
//  WBWelcomeView.swift
//  个人微博
//
//  Created by 任聪聪 on 2018/1/10.
//  Copyright © 2018年 任孜聪. All rights reserved.
//

import UIKit
//微博欢迎界面
class WBWelcomeView: UIView {
    
    @IBOutlet weak var iconView: UIImageView!
    //底部约束
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    class func welcomeView()->WBWelcomeView{
        let nib = UINib(nibName: "WBWelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        //使用XIB的加载的视图默认是600X600的
        v.frame = UIScreen.main.bounds
        return v
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    /// 设置iconView头像
    override func awakeFromNib() {
        //找到头像路径
        guard let urlString = WBNetWorkManager.shared.userAccount.avatar_large,let url = URL(string: urlString)  else {
            return
        }
        iconView.setImageWith(url, placeholderImage: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = iconView.bounds.width*0.5
        iconView.layer.masksToBounds = true
    }
    //视图已经加载到屏幕上的时候
    override func didMoveToWindow() {
        //视图是用自动布局设置，只是设置了约束
        //当视图被添加到窗口上时，根据父视图的大小计算约束值，更新控件位置
        super.didMoveToWindow()
        //将之前的约束条件更新，更新控件位置
        //此时位置就是XIB布局的原始原始位置
        self.layoutIfNeeded()
        //重新设置头像的底部约束
        bottomConstraint.constant = bounds.size.height - 200
        //界面动画设置usingSpringWithDamping->弹力系数，initialSpringVelocity->初始速度
        //如果控件的Frame还没有计算好，所有约束就会一起动画
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.layoutIfNeeded()
        }) { (_) in
            //显示欢迎归来
            UIView.animate(withDuration: 1.0, animations: {
                self.tipLabel.alpha = 1
            }, completion: { (_) in
                self.removeFromSuperview()
            })
        }
    }
}
